package alg20250224;

import java.util.ArrayList;
import java.util.List;

public class Text {
    public List<Integer> findAnagrams(String ss, String pp) {
        char[] s = ss.toCharArray();
        char[] p = pp.toCharArray();
        int[] hash1 = new int[128];
        int[] hash2 = new int[128];
        List<Integer> ret = new ArrayList<>();
        for (int i = 0; i < p.length; i++) {
            hash2[p[i]]++;
        }
        for (int left = 0, right = 0, count = 0; right < s.length; right++) {
            //进窗口
            hash1[s[right]]++;
            if(hash1[s[right]] <= hash2[s[right]]) count++; //维护count
            //判断
            if(right - left + 1 > p.length){
                //出窗口
                if(hash1[s[left]] <= hash2[s[left]]) count--; //维护count
                hash1[s[left++]]--;
            }
            //更新结果
            if(count == p.length) ret.add(left);
        }
        return ret;
    }
}
